﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TInventoryRepository : BaseRepository<TInventory>, ITInventoryRepository
    {
        public TInventoryRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        public List<TInventoryDto> GetTInventoryDtoPage(out int totalCount, int? owerId, int? whId, string? wlName, int? kcState, int pageIndex = 1, int pageSize = 10)
        {
            var list = Context.TInventories.AsNoTracking();
            if (owerId.HasValue)
            {
                list = list.Where(s => s.OwnerId == owerId);
            }
            if (whId.HasValue)
            {
                list = list.Where(s => s.WarehouseId == whId);
            }
            if (!string.IsNullOrEmpty(wlName))
            {
                list = list.Where(s => s.MaterialName == wlName);
            }
            if (kcState.HasValue)
            {
                list = list.Where(s=>s.Status==kcState);
            }
            totalCount = list.Count();
            // 分页处理
            var paginatedList = list.OrderByDescending(s => s.CreateDate)
                                    .Skip((pageIndex - 1) * pageSize)
                                    .Take(pageSize)
                                    .AsNoTracking()
                                    .ToList()
                                    .Select(s => new TInventoryDto()
                                    {
                                        Id = s.Id,
                                        PalletBarcode = s.PalletBarcode,
                                        LocationName = s.LocationName,
                                        MaterialName = s.MaterialName,
                                        AvailableQty = s.AvailableQty,
                                        StatusVal = GetInventoryStatus(s.Status),
                                        MaterialCode = s.MaterialCode,
                                        TotalPrice = s.TotalPrice,
                                        Price = s.Price,
                                        BatchNo = s.BatchNo,
                                        BatchNoExplain = s.BatchNoExplain,
                                        LocationCode = s.LocationCode,
                                        ErplocationCode = s.ErplocationCode,
                                        OwnerName = s.OwnerName,
                                        AreaName = s.AreaName,
                                        SupplierName = s.SupplierName,
                                        ExpireDate = s.ExpireDate,
                                        ProductDate = s.ProductDate,
                                        InventoryDetailDtos = GetInventoryDetailDtos(s.Id)
                                    })
                                    .ToList();
            return paginatedList;
        }
        public string GetInventoryStatus(int? status)
        {
            string inventoryStatus;

            switch (status)
            {
                case 1:
                    inventoryStatus = "正常库存";
                    break;
                case 2:
                    inventoryStatus = "转移中的库存";
                    break;
                case 3:
                    inventoryStatus = "入库在途";
                    break;
                case 4:
                    inventoryStatus = "出库在途";
                    break;
                case 5:
                    inventoryStatus = "限制使用的库存";
                    break;
                case 6:
                    inventoryStatus = "异常库存";
                    break;
                case 7:
                    inventoryStatus = "预分配库存";
                    break;
                case 8:
                    inventoryStatus = "冻结库存";
                    break;
                default:
                    inventoryStatus = "无";
                    break;
            }

            return inventoryStatus;
        }

        private List<InventoryDetailDto>? GetInventoryDetailDtos(int kqId)
        {
            return Context.TInventoryDetails.AsNoTracking().Where(s => s.InventoryId == kqId).Select(s=>new InventoryDetailDto()
            {
                PackageBarcode=s.PackageBarcode,
                Barcode=s.Barcode,
                SkuNum=s.SkuNum,
            }).ToList();
        }

        public List<TInventory> GetTInventoryPage(out int totalCount, int? owerId, int? whId, string? wlName, int pageIndex = 1, int pageSize = 10)
        {
            var list = Context.TInventories.AsNoTracking();

            if (owerId.HasValue)
            {
                list = list.Where(s => s.OwnerId == owerId);
            }
            if (whId.HasValue)
            {
                list = list.Where(s => s.WarehouseId == whId);
            }
            if (!string.IsNullOrEmpty(wlName))
            {
                list = list.Where(s => s.MaterialName.Contains(wlName));
            }
            
            var res = list.OrderByDescending(x => x.CreateDate)
                .OrderBy(x => x.CreateDate)
                        .GroupBy(s => s.MaterialId)
                        .Select(g => new TInventory()
                        {
                            Id = g.First().Id,
                            MaterialCode = g.First().MaterialCode,
                            MaterialName = g.First().MaterialName,
                            OwnerName = g.First().OwnerName,
                            TotalPrice = g.First().TotalPrice,
                            AvailableQty = g.Sum(s => s.AvailableQty)
                        }).ToList();

            totalCount = res.Count();
            // 分页处理
            var paginatedList = res.Skip((pageIndex - 1) * pageSize)
                        .Take(pageSize);
            return paginatedList.ToList(); ;
        }

        /// <summary>
        /// 查询所有库存表信息（非分页）
        /// </summary>
        /// <returns></returns>
        public List<TInventory> GetInventoryList()
        {
            var list = Context.TInventories.AsNoTracking();
            return list.OrderBy(r => r.Id).ToList();
        }
        /// <summary>
        /// 导出库存
        /// </summary>
        /// <returns></returns>
        public List<TInventory> GetTInventoryExcel()
        {
            try
            {
                return Context.TInventories.AsNoTracking().ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<TInventory> GetTInventoryid(int id)
        {
            throw new NotImplementedException();
        }
    }
}
